jQuery(document).ready(function()
{
    _loading(0,'');
    _init_control();
    _init_tabs();
    _loading(1,'')

});
var tabs=new Object;
//Init tabs
function _init_tabs()
{
    _load_manage_mail();
    _load_manage_mail_view_dialog();
    tabs[0]=true;
    $('#mail_system_manage').tabs({  
        border:false,  
        onSelect:function(title,index){
            if(tabs[index]!=true)
                {
                switch(index)
                {                    
                    case 1:
                        _load_manage_log();
                        _load_manage_log_view_dialog();
                        break;
                    case 2:
                        _load_manage_maddress();

                        break;
                    default:
                }
                tabs[index]=true;
            }         
        }
    }); 
}
//Top tool
function _send_all_mail()
{
    _loading(0,'Sending');
    url="/auto_sendmail/index.php";
    jQuery.get(url, function(data) {
        if(data=="OK")
            {
            _loading(3,'Send mail success!');
            setTimeout(function(){_loading(1,'')},3000);
        }
        else
            _loading(3,'Send mail failed!');
        _reload_all();
        setTimeout(function(){_loading(1,'')},3000)
    });
}
function _reload_all()
{
    jQuery("#_manage_mails").datagrid("reload");
    jQuery("#_manage_template").datagrid("reload");
    jQuery.getJSON('/index.php?module=mailsystemmodule&action=templates_manage_json&ajax_action=1&reload_var=1', function(data) {_temp=data});
}
function _loading(action,mes)
{
    if(action==0)
        {
        jQuery('#loading_mes').html(mes);
        jQuery.fancybox('#loading_win');
    }
    else if(action==1)
        {
        jQuery.fancybox.close();
    }
    else
        {
        jQuery('#loading_mes').html(mes);
    }

}

function _init_control()
{
    //Mail manage control
    jQuery('#_mail_search').searchbox({  
        searcher:function(value,name){ 
            jQuery("#_manage_mails").datagrid("reload");
        },  
        menu:'#_mail_search_menu',  
        prompt:'Please Input Value'  
    });
    jQuery('#_mail_fd').datetimebox({  
        showSeconds:false,
        mulitple: false,
        editable: false
    });
    jQuery('#_mail_td').datetimebox({  
        showSeconds:false,
        mulitple: false,
        editable: false
    }); 
    jQuery('#_mail_combo_bl').combobox({  
        mulitple: false,
        editable: false,
        panelHeight:100,
        onSelect: function(param){
            if(jQuery(this).combobox('getValue')==2)
                jQuery('#_mail_resend').linkbutton('enable');
            else
                jQuery('#_mail_resend').linkbutton('disable');
            jQuery("#_manage_mails").datagrid("reload");
        }
    }); 
    jQuery('#_mail_group').combobox({  
        mulitple: false,
        editable: false,
        panelHeight:100,
        onSelect: function(param){
            jQuery("#_manage_mails").datagrid("reload");
        }
    });


    
    //Log control
    jQuery('#_log_search').searchbox({  
        searcher:function(value,name){ 
            jQuery("#_manage_log").datagrid("reload");
        },  
        menu:'#_log_search_menu',  
        prompt:'Please Input Value'  
    });  
    jQuery('#_log_type').combobox({  
        mulitple: false,
        editable: false,
        panelHeight:100,
        onSelect: function(param){
            jQuery("#_manage_log").datagrid("reload");
        }
    });   
    jQuery('#_log_fd').datetimebox({  
        showSeconds:false,
        mulitple: false,
        editable: false
    });
    jQuery('#_log_td').datetimebox({  
        showSeconds:false,
        mulitple: false,
        editable: false
    }); 
    //Mail address control
    jQuery('#_maddress_search').searchbox({  
        searcher:function(value,name){ 
            jQuery("#_manage_maddress").datagrid("reload");
        },
        menu:'#_maddress_search_menu',   
        prompt:'Please Input Value'  
    });  
    jQuery('#_maddress_block').combobox({  
        mulitple: false,
        editable: false,
        panelHeight:100,
        onSelect: function(param){
            jQuery("#_manage_maddress").datagrid("reload");
        }
    });   
    jQuery('#_maddress_fd').datetimebox({  
        showSeconds:false,
        mulitple: false,
        editable: false
    });
    jQuery('#_maddress_td').datetimebox({  
        showSeconds:false,
        mulitple: false,
        editable: false
    }); 
}

//Mail manage
function _load_manage_mail_reload()
{
    jQuery("#_manage_mails").datagrid("reload");
}
function _load_manage_mail(){
    jQuery('#_manage_mails').datagrid(
    {
        autoRowHeight: true, 
        idField: "id",
        nowrap: false,
        height: 465,
        pageNumber: 1,
        pageSize: 10,
        pagination: 1,
        rownumbers: true,
        singleSelect: true, 
        columns:[[  
        {field:'email_to',title:'Send to',width:140, sortable:true},  
        {field:'subject',title:'Subject',width:330, sortable:true},
        {field:'group',title:'Group',width:140, sortable:true}, 
        {field:'date_insert_formater',title:'Send to',width:160, sortable:true}, 

        ]]  ,
        onBeforeLoad: function(param){


            var url="/index.php?module=mailsystemmodule&action=mails_manage_json&ajax_action=1";
            var options=jQuery(this).datagrid("options");
            var search_name=jQuery('#_mail_search').searchbox('getName');
            var search_detail=jQuery('#_mail_search').searchbox('getValue');

            if((search_name!='')&&(search_detail!='')){
                url += "&search_name="+search_name+"&search_detail="+search_detail;
            }
            var group_email=jQuery.trim(jQuery("#_mail_group").combobox("getValue"));
            if(group_email!='')
                {
                url += "&group="+group_email;   
            }
            var mail_status = $('#_mail_combo_bl').combobox('getValue');
            if(mail_status!="")
                {
                url+="&mail_status="+mail_status;
            }
            var from_date = $('#_mail_fd').datetimebox('getValue');
            if(from_date!="")
                {
                url+="&from_date="+from_date;
            }
            var to_date = $('#_mail_td').datetimebox('getValue');
            if(to_date!="")
                {
                url+="&to_date="+to_date;
            }

            if (options.sortName != null){
                url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
            }
            jQuery(this).datagrid('options').url = url;

        },
        onDblClickRow: function(rowIndex, rowData){
            __mail_View();
        }
    }); 
}
//Mail dialog
function __mail_Edit(add_new)
{
    var mail_status = $('#_mail_combo_bl').combobox('getValue');    
    var url="/index.php?module=mailsystemmodule&mail_status="+mail_status+"&action=mails_manage_edit";
    
    var node = jQuery("#_manage_mails").datagrid('getSelected');
    
    if(add_new!=false)
        {
        url+="&default_temp="+add_new;
    }
    else
    {
        url+="&id="+node.id;
    }
     wopts  = 'width=500,height=650,resizable=1,alwaysRaised=1,scrollbars=1,top=20,left=200';
    var child=window.open(url, 'New email', wopts);

}
function __mail_View()
{
    
    var mail_status = $('#_mail_combo_bl').combobox('getValue');    
    var url="/index.php?module=mailsystemmodule&action=mails_manage_view&ajax_action=1";

    var node = jQuery("#_manage_mails").datagrid('getSelected');
    if (node==null)
        {
        return false;
    }
    else
        {
        url+="&id="+node.id+"&mail_status="+mail_status+"";  
        SetWindowPanelTitle("_mail_view", "View <"+node.subject+">");        
    }
    jQuery.ajax({
        url: url,
        success: function(data) {
            jQuery("#_mail_view_content").html(data);

        },
        async:false
    });

    jQuery('#_mail_view').dialog("open");
}
function _load_manage_mail_view_dialog()
{
    $('#_mail_view').dialog({
        title:'View log',
        width: 600,
        height: 350,
        closed: true,  
        modal: true,
        cache: false,
        buttons:[{
            text:'Ok',
            iconCls:'icon-ok',
            handler:function(){
                $('#_mail_view').dialog('close');              
            }
        }]
    });
}
function __mail_Resend()
{
        _loading(0,'Resending...');
    
    var mail_status = $('#_mail_combo_bl').combobox('getValue');   
    if(mail_status!=2)
    return false; 
    var node = jQuery("#_manage_mails").datagrid('getSelected');
    jQuery.get('/index.php?module=mailsystemmodule&ajax_action=1&action=mails_manage_resend&id='+node.id, function(data) {
                if(data=='OK')
                {
                    $('#_mail_combo_bl').combobox('select', 0);
                     jQuery("#_manage_mails").datagrid("reload");
                    _loading(3,'Resend success!');
                    setTimeout(function(){_loading(1,'')},3000);
                }
                else
                {
                     _loading(3,'Send mail failed!');
                     setTimeout(function(){_loading(1,'')},3000);
                }                   
            }); 
 
}

function __mail_Remove()
{
    var mail_status = $('#_mail_combo_bl').combobox('getValue');    
    var node = jQuery("#_manage_mails").datagrid('getSelected');
    if(node!=null)
        $.messager.confirm('Delete mail', 'Are you sure deleting this item?', function(conf){  
        if (conf){  
            $.get('/index.php?module=mailsystemmodule&ajax_action=1&mail_status='+mail_status+'&action=mails_manage_delete&id='+node.id, function(data) {
                if(data=='OK')
                    jQuery("#_manage_mails").datagrid("reload");
            }); 
        }  
    });  
}

//Log manage
function _load_manage_log()
{
    jQuery('#_manage_log').datagrid({
        autoRowHeight: true, 
        idField: "id",
        nowrap: false,
        height: 465,
        pageNumber: 1,
        pageSize: 10,
        pagination: 1,
        rownumbers: true,
        singleSelect: true, 
        columns:[[  
        {field:'email',title:'Email',width:160, sortable:true},  
        {field:'date',title:'Date',width:150, sortable:true},
        {field:'type',title:'Type',width:140, sortable:true}, 
        {field:'description',title:'Description',width:240, sortable:true}, 
        ]]  ,
        onBeforeLoad: function(param){
            var url="/index.php?module=mailsystemmodule&action=log_manage_json&ajax_action=1";
            var options=jQuery(this).datagrid("options");            
            var search_name=jQuery('#_log_search').searchbox('getName');
            var search_detail=jQuery('#_log_search').searchbox('getValue');

            if((search_name!='')&&(search_detail!='')){
                url += "&search_name="+search_name+"&search_detail="+search_detail;
            }

            var type=jQuery.trim(jQuery("#_log_type").combobox("getValue"));
            if(type!='')
                {
                url += "&type="+type;   
            }

            var from_date = $('#_log_fd').datetimebox('getValue');
            if(from_date!="")
                {
                url+="&from_date="+from_date;
            }
            var to_date = $('#_log_td').datetimebox('getValue');
            if(to_date!="")
                {
                url+="&to_date="+to_date;
            }

            if (options.sortName != null){
                url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
            }
            jQuery(this).datagrid('options').url = url;

        },
        onDblClickRow: function(rowIndex, rowData){
            __log_View();
        }
    }); 
}
function _load_manage_log_view_dialog()
{
    $('#_log_view').dialog({
        title:'View log',
        width: 600,
        height: 350,
        closed: true,  
        modal: true,
        cache: false,
        buttons:[{
            text:'Ok',
            iconCls:'icon-ok',
            handler:function(){
                $('#_log_view').dialog('close');              
            }
        }]
    });
}
function __log_View()
{
    
    var url="/index.php?module=mailsystemmodule&action=log_manage_view&ajax_action=1";

    var node = jQuery("#_manage_log").datagrid('getSelected');
    if (node==null)
        {
        return false;
    }
    else
        {
        url+="&id="+node.id;  
        SetWindowPanelTitle("_log_view", "View "+node.id+" log");        
    }
    jQuery.ajax({
        url: url,
        success: function(data) {
            jQuery("#_log_view_content").html(data);

        },
        async:false
    });
    
    jQuery('#_log_view').dialog("open");
}
function __log_Remove()
{
    var node = jQuery("#_manage_log").datagrid('getSelected');
    if(node!=null)
        $.messager.confirm('Delete log', 'Are you sure deleting this log?', function(conf){  
        if (conf){  
            $.get('/index.php?module=mailsystemmodule&ajax_action=1&action=log_manage_delete&id='+node.id, function(data) {
                if(data=='OK')
                    jQuery("#_manage_log").datagrid("reload");
            }); 
        }  
    });  
}

function __log_ClearAll()
{
    $.messager.confirm('Clear all log', 'Are you sure clear all log?', function(conf)
    {  
        if (conf)
        {  
            $.get('/index.php?module=mailsystemmodule&ajax_action=1&action=log_manage_delete&clearall=1', function(data) {
                if(data=='OK')
                    jQuery("#_manage_log").datagrid("reload");
            }); 
        }
    });  
}

//Mail address
function _load_manage_maddress()
{
    jQuery('#_manage_maddress').datagrid({
        autoRowHeight: true, 
        idField: "id",
        nowrap: false,
        height: 465,
        pageNumber: 1,
        pageSize: 10,
        pagination: 1,
        rownumbers: true,
        singleSelect: true, 
        columns:[[  
        {field:'email',title:'Email',width:140, sortable:true},  
        {field:'last_send',title:'Last send',width:150, sortable:true},
        {field:'count',title:'Count',width:140, sortable:true}, 
        {field:'count_error',title:'Count error',width:140, sortable:true}, 

        {field:'block',title:'Was block',width:140, sortable:true,formatter: function(value){if (value==0) return 'Not block'; else return 'Has Been block';}},
        ]]  ,
        onBeforeLoad: function(param){
            var url="/index.php?module=mailsystemmodule&action=mailaddress_manage_json&ajax_action=1";
            var options=jQuery(this).datagrid("options");            
            var search_name=jQuery('#_maddress_search').searchbox('getName');
            var search_detail=jQuery('#_maddress_search').searchbox('getValue');

            if((search_name!='')&&(search_detail!='')){
                url += "&search_name="+search_name+"&search_detail="+search_detail;
            }

            var block=jQuery.trim(jQuery("#_maddress_block").combobox("getValue"));
            if(block!='')
                {
                url += "&block="+block;   
            }

            var from_date = $('#_maddress_fd').datetimebox('getValue');
            if(from_date!="")
                {
                url+="&from_date="+from_date;
            }
            var to_date = $('#_maddress_td').datetimebox('getValue');
            if(to_date!="")
                {
                url+="&to_date="+to_date;
            }

            if (options.sortName != null){
                url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
            }
            jQuery(this).datagrid('options').url = url;

        }
    }); 
}

function _load_manage_maddress_view_dialog()
{

}

function __maddress_block()
{
    var node = jQuery("#_manage_maddress").datagrid('getSelected');
    if(node!=null)       
        $.get('/index.php?module=mailsystemmodule&ajax_action=1&action=mailaddress_manage_block&block=1&id='+node.id, function(data) {
        if(data=='OK')
            jQuery("#_manage_maddress").datagrid("reload");
    });  
}
function __maddress_unblock()
{
    var node = jQuery("#_manage_maddress").datagrid('getSelected');
    if(node!=null)       
        $.get('/index.php?module=mailsystemmodule&ajax_action=1&action=mailaddress_manage_block&id='+node.id, function(data) {
        if(data=='OK')
            jQuery("#_manage_maddress").datagrid("reload");
    });  
}
function SetWindowPanelTitle(dialog_id, title){
    jQuery('#'+dialog_id).dialog("window").find(".panel-title:first").html(title);
}

